//
//  SCSetLoginPwdController.m
//  Scalper
//
//  Created by 王云 on 2017/11/27.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "SCSetLoginPwdController.h"
#import "FirstViewController.h"

@interface SCSetLoginPwdController ()

@end

@implementation SCSetLoginPwdController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"设置登录密码";
    
    _PromptL.attributedText = [WYNSString Att:@"温馨提示:\n第三方账号（如微信账号）绑定过的手机号，可直接通过手机号来登录黄牛派，如之前未设置过登录密码，请通过本界面功能，来初始化设置登录密码。" rang:@"温馨提示:" font:[UIFont systemFontOfSize:15] color:KOrange ParagraphSpacing:8];
    
    _phoneF.userInteractionEnabled = NO;
    _phoneF.text = [SCUserTool loadInfo].member_mobile;
}

- (IBAction)verClick:(id)sender {
    
    [self.view endEditing:YES];
    
    __block NSInteger timeout=59;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    
    [SCApiTool ScSendPhone:_phoneF.text type:@"set_password" success:^(NSString *verStr) {
        [SCTool showSuccess:verStr];
        
        dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0);
        dispatch_source_set_event_handler(_timer, ^{
            if(timeout<=0){
                dispatch_source_cancel(_timer);
                dispatch_async(dispatch_get_main_queue(), ^{
                    [_verButton setTitle:@"获取验证码" forState:UIControlStateNormal];
                    _verButton.userInteractionEnabled = YES;
                    [_verButton setBackgroundColor:KOrange];
                });
            }else{
                NSInteger seconds = timeout % 60;
                NSString *strTime = [NSString stringWithFormat:@"%.2ld", (long)seconds];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [_verButton setTitle:[NSString stringWithFormat:@"%@秒后重发",strTime] forState:UIControlStateNormal];
                    _verButton.userInteractionEnabled = NO;
                    [_verButton setBackgroundColor:WYColor(215, 215, 215)];
                });
                timeout--;
                
            }
        });
        dispatch_resume(_timer);
        
    } failure:^(NSString *msg) {
        [SCTool showError:msg];
        [_verButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        _verButton.userInteractionEnabled = YES;
        _verButton.backgroundColor = KOrange;
    }];
    
    
}

- (IBAction)regClick:(id)sender {
    
    if (_pwdF.text.length < 6) {
        [SCTool showError:@"密码输入过短"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [_pwdF becomeFirstResponder];
        });
        return;
    }
    
    if (_verF.text.length == 0) {
        [SCTool showError:@"验证码错误"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [_verF becomeFirstResponder];
        });
        return;
    }
    
    
    __weak __typeof(&*self)weakSelf = self;
    
    [SCApiTool ScForgotpasswordPhone:_phoneF.text code:_verF.text password:_pwdF.text success:^(NSString *msg) {
        [SCTool showMessage:@"修改成功,请重新登录"];
        [SCUserTool logOut];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIApplication sharedApplication].delegate.window.rootViewController =[[JTNavigationController alloc] initWithRootViewController:[[FirstViewController alloc] init]];
        });
        
    } failure:^(NSString *errormsg) {
        [SCTool showError:errormsg];
        _verButton.backgroundColor = KOrange;
        [weakSelf.verButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        weakSelf.verButton.userInteractionEnabled = YES;
    }];

   
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [_verButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    _verButton.userInteractionEnabled = YES;
    _verButton.backgroundColor = KOrange;
}


#pragma mark - textFiled delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField == _pwdF){
        
        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
        if (existedLength - selectedLength + replaceLength > 20) {
            return NO;
        }
    }
    else if (textField == _verF){
        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
        if (existedLength - selectedLength + replaceLength > 10) {
            return NO;
        }
        
    }
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
